package NullObject.Before;

/**
 * Our video game character carries a shield. When we get attacked we want to
 * make sure that our shield takes damage before we do. At this point though
 * our character doesn't have a shield.  Here is an example of how we might
 * deal with that.
 */
public class Game
{

}

class Player
{
    int lifeLeft = 100;
    Shield shield;

    void playerAttacked( int damage )
    {
        int excessDamage = 0;       // This is damage beyond what the shield can take.

        if( shield != null )        // Make sure we actually have a shield first.
            excessDamage = shield.takeDamage( damage );

        takeDamage( excessDamage );
    }

    void takeDamage( int damage )
    {
        lifeLeft = lifeLeft - damage;
        if( lifeLeft < 0 )         // You are dead.
            lifeLeft = 0;
    }
}

class Shield
{
    int shieldHitPoints;

    Shield( int sheildHitPoints )
    {
        this.shieldHitPoints = shieldHitPoints;
    }

    public int takeDamage( int damage )
    {
        shieldHitPoints = shieldHitPoints - damage;
        if( shieldHitPoints < 0 )
        {
            shieldHitPoints = 0;    // Make our shield worth nothing.
            return shieldHitPoints; // return the damage that goes beyond what the shield can handle.
        }
        return 0;                   // The shield could hold all the damage.
    }
}

